import { clone } from '@/utils/index'
import { statusOk, findByName, findById, nameById } from '../find'
import { getBrandChannel, setBrandChannel, removeBrandChannel } from '@/utils/auth'


// 品牌或购买渠道数组
const state = getBrandChannel()

const getters = {
	statusOk,
	findByName,
	findById,
	nameById,
	/**
 * @example 全部品牌
 * @returns {Array} brands 全部品牌
 */
	brands: (state, getters) => {
		const all = getters.statusOk
		const res = []
		for (const item of all) {
			const { type } = item
			if (type === 'brand') res.push(item)
		}
		return clone(res)
	},
	/**
	 * @example 购买渠道
	 * @returns {Array} channels 购买渠道
	 */
	channels: (state, getters) => {
		const all = getters.statusOk
		const res = []
		for (const item of all) {
			const { type } = item
			if (type === 'channel') res.push(item)
		}
		return clone(res)
	}
}


const mutations = {
	/**
	 * @example 功能：品牌或购买渠道数组
	 * @param {Array} data 品牌或购买渠道
	 * @returns {} 无
	 */
	SET_INIT: (state, data) => {
		if (!Array.isArray(data)) return console.warn('品牌或购买渠道', data)
		setBrandChannel(data)
		state.splice(0, Infinity, ...data)
	},
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
}